/**
 * jQuery Nadeb lightBox plugin
 *
 *
 * @name    jquery.nadeb.lightbox1.0.0.js
 * @author  Mateus Martins
 * @version 1.0.0
 * @date    14.07.2010
 * @category jQuery plugin
 * @copyright (c) Mateus Martins
 * @license CC Attribution-No Derivative Works 2.5 Brazil - http://creativecommons.org/licenses/by-nd/2.5/br/deed.en_US
 * @example $('class_or_id_to_tag_link_for_image').nadebLightBox();
 */

// Offering a Custom Alias suport - More info: http://docs.jquery.com/Plugins/Authoring#Custom_Alias
(function($)
{
	$.fn.nadebLightBox = function(settings)
	{
		var config = {
				'bgColor'    : '#000',
				'bgOpacity'  : 0.5,
				'urlLoader'  : 'http://nadeb-lightbox.googlecode.com/svn/trunk/img/loading.gif',
				'close'      : 'Fechar'
		};
		if (settings) $.extend(config, settings);

		this.click(function()
		{
			var imgSr  = "";
			var imgSrc = $(this).attr('href');
			var img    = new Image();

			$('body').append('<div class="ltBox_bg"></div>');
			$('body').append('<div class="ltBox_window"><div class="barra">'+ config.close +'</div>');
			$('body').append('<div class="ltBox_container"></div>');

			$('.ltBox_window .barra').live('click',function(){
				/*
				$('.ltBox_bg').fadeTo('slow',0,function(){$('.ltBox_bg').remove()});
				$('.ltBox_window').fadeTo('fast',0,function(){$('.ltBox_window').remove()});
				$('.ltBox_container').fadeTo('fast',0,function(){$('.ltBox_container').remove()});
				*/
				$('.ltBox_bg').remove();
				$('.ltBox_window').remove();
				$('.ltBox_container').remove();
			});

			$('.ltBox_bg').fadeTo(0,0);
			$('.ltBox_window').fadeTo(0,0);

			$('.ltBox_bg').css('background-color',config.bgColor);
			$('.ltBox_bg').css('position','fixed');
			$('.ltBox_bg').css('height','100%');
			$('.ltBox_bg').css('width','100%');
			$('.ltBox_bg').css('left','0');
			$('.ltBox_bg').css('top','0');

			$('.ltBox_window').css('background','#fff url('+ config.urlLoader +') center no-repeat');
			$('.ltBox_window').css('position','fixed');
			$('.ltBox_window').css('height','150px');
			$('.ltBox_window').css('width','150px');

			$('.ltBox_window .barra').css('font','14px Verdana,Helvetica,sans-serif');
			$('.ltBox_window .barra').css('padding-bottom','10px');
			$('.ltBox_window .barra').css('padding-top','10px');
			$('.ltBox_window .barra').css('padding-right','10px');
			$('.ltBox_window .barra').css('float','right');
			$('.ltBox_window .barra').css('cursor','pointer');

			$('.ltBox_container').css('position','fixed');

			$('.ltBox_window').css('left', ( $('.ltBox_bg').width() - $('.ltBox_window').width() ) / 2 );
			$('.ltBox_window').css('top', ( $('.ltBox_bg').height() - $('.ltBox_window').height() ) / 2 );

			$('.ltBox_bg').fadeTo('fast',config.bgOpacity, function()
			{
				$('.ltBox_window').fadeTo('slow',1, function()
				{
					$(img).load(function ()
					{
						var imgWidth   = this.width;
					    var imgHeight  = this.height;
						var sobraA     = 0;
						var sobraB     = 0;

						$('.ltBox_container').css('left', ( $('.ltBox_bg').width() - (imgWidth) ) / 2);
						$('.ltBox_container').css('top', ( $('.ltBox_bg').height() - (imgHeight - 25) ) / 2);

						if( $('.ltBox_bg').width() < imgWidth )
						{
							imgWidth = $('.ltBox_bg').width() - 60;
							$('.ltBox_container').css('left', ( $('.ltBox_bg').width() - (imgWidth) ) / 2);
							$('.ltBox_container').css('width', imgWidth);
							$('.ltBox_container').css('overflow', 'auto');
						}
						if( $('.ltBox_bg').height() < imgHeight )
						{
							imgHeight = $('.ltBox_bg').height() - 70;
							$('.ltBox_container').css('top', ( $('.ltBox_bg').height() - (imgHeight -25) ) / 2);
							$('.ltBox_container').css('height', imgHeight);
							$('.ltBox_container').css('overflow', 'auto');

						}

						if( $('.ltBox_bg').height() < this.height && $('.ltBox_bg').width() > this.width )
						{
							sobraA = 20;
							$('.ltBox_container').css('width', imgWidth + 22);
						}

						if( $('.ltBox_bg').height() > this.height && $('.ltBox_bg').width() < this.width )
						{
							sobraB = 20;
							$('.ltBox_container').css('height', imgHeight + 22);
						}

						$('.ltBox_window').animate({
							width : imgWidth + 20 + sobraA,
							height: imgHeight + 40 + sobraB,
							left  : ( $('.ltBox_bg').width() - (imgWidth + 20) ) / 2,
							top   : ( $('.ltBox_bg').height() - (imgHeight + 40) ) / 2
						},{
							duration: 500,
							complete: function() {
								setTimeout(
										function()
										{
											$('.ltBox_container').html( '<img src="'+ imgSrc +'" alt="-" />' );
											$('.ltBox_container').fadeTo(0,0,function() {
												$('.ltBox_container').fadeTo('slow',1);
											});

										}
										, 1000
									)
							}
						});
				    }).attr('src', imgSrc );
				});
			});

			return false;
		});

		return this;
   };
})(jQuery);